﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using G15_Agent.Models.Transport;
using G15_Agent.Helpers.ServiceHelpers;
using System.Web.Mvc;

namespace G15_Agent.Helpers.ServiceHelpers.TransportServiceHelpers
{
    public class TransportService : Service
    {
        virtual public List<string> IGetVehicleBrand()
        {
            return null;
        }

        virtual public List<LoaiXe> IGetTransportType()
        {
            return null;
        }

        virtual public List<string> IGetSeatOption()
        {
            return null;
        }

        virtual public List<string> IGetServiceType()
        {
            return null;
        }

        virtual public List<ChiTietXe> ISearchByTypeOfService(string serviceType)
        {
            return null;
        }
        
        virtual public List<ChiTietXe> ISearchBySeat(string numberOfSeat)
        {
            return null;
        }

        virtual public List<BangGiaXeDuLich> IRetrieveFeeByRegion(string region)
        {
            return null;
        }

        virtual public List<BangGiaTheoGio> IRetrieveFeeByService(string service)
        {
            return null;
        }

        virtual public List<LoTrinh> IRetrieveRoute()
        {
            return null;
        }

        virtual public List<string> IRetrieveRegionList()
        {
            return null;
        }

        virtual public List<LoaiXe> IRetrieveVehicleList()
        {
            return null;
        }

        virtual public List<LoaiXe> ISearchForTransportation(
            string region,
            string classID,
            string numberOfSeat,
            string service)
        {
            return null;
        }
        virtual public int? IBookingForTransportation(
            string verhicleName,
            string service,
            DateTime departDate,
            DateTime returnDate,
            int driver,
            int numberOfSeat)
        {
            return null;
        }

// ham su dung
        public List<LoaiXe> GetTransportType()
        {
            string key =
                "serviceID" + this.ServiceID +
                "GetTransportType";
            List<LoaiXe> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<LoaiXe>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.IGetTransportType();
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.IGetTransportType();
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }
        public List<ChiTietXe> SearchByTypeOfService(string serviceType)
        {
            string key =
                "serviceID" + this.ServiceID +
                "serviceType" + serviceType;

            List<ChiTietXe> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<ChiTietXe>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.ISearchByTypeOfService(serviceType);
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.ISearchByTypeOfService(serviceType);
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }
        public List<ChiTietXe> SearchBySeat(string numberOfSeat)
        {
            string key =
                "serviceID" + this.ServiceID +
                "numberOfSeat" + numberOfSeat;

            List<ChiTietXe> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<ChiTietXe>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.ISearchBySeat(numberOfSeat);
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.ISearchBySeat(numberOfSeat);
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }
        public List<BangGiaXeDuLich> RetrieveFeeByRegion(string region)
        {
            string key =
                "serviceID" + this.ServiceID +
                "region" + region;

            List<BangGiaXeDuLich> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<BangGiaXeDuLich>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.IRetrieveFeeByRegion(region);
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.IRetrieveFeeByRegion(region);
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }
        public List<BangGiaTheoGio> RetrieveFeeByService(string service)
        {
            string key =
                "serviceID" + this.ServiceID +
                "serviceType" + service;

            List<BangGiaTheoGio> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<BangGiaTheoGio>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.IRetrieveFeeByService(service);
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.IRetrieveFeeByService(service);
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }
        public List<LoTrinh> RetrieveRoute()
        {
            string key =
                "serviceID" + this.ServiceID +
                "RetrieveRoute";

            List<LoTrinh> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<LoTrinh>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.IRetrieveRoute();
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.IRetrieveRoute();
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }
        public List<SelectListItem> RetrieveRegionList()
        {
            string key =
                "serviceID" + this.ServiceID +
                "RetrieveRegionList";

            List<string> listRegion = null;
            // Kiểm tra dữ liệu trong cache
            listRegion = (List<string>)HttpRuntime.Cache[key];
            if (listRegion == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    listRegion = this.IRetrieveRegionList();
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        listRegion = this.IRetrieveRegionList();
                }
                finally
                {
                    if (listRegion != null)
                        HttpRuntime.Cache[key] = listRegion;
                }
            }

            List<SelectListItem> result = null;

            if (listRegion != null)
            {
                result = new List<SelectListItem>();
                //result.Add(new SelectListItem
                //{
                //    Value = @"",
                //    Text = @"Bất kì"
                //});
                foreach (var item in listRegion)
                {
                    result.Add(new SelectListItem
                    {
                        Text = item,
                        Value = item
                    });
                }
            }

            return result;
        }
        public List<LoaiXe> RetrieveVehicleList()
        {
            string key =
                "serviceID" + this.ServiceID +
                "RetrieveVehicleList";

            List<LoaiXe> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<LoaiXe>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.IRetrieveVehicleList();
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.IRetrieveVehicleList();
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }
        public List<SelectListItem> RetrieveVehicleBrand()
        {
            string key =
                "serviceID" + this.ServiceID +
                "RetrieveVehicleBrand";

            List<string> listBrand = null;
            // Kiểm tra dữ liệu trong cache
            listBrand = (List<string>)HttpRuntime.Cache[key];
            if (listBrand == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    listBrand = this.IGetVehicleBrand();
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        listBrand = this.IGetVehicleBrand();
                }
                finally
                {
                    if (listBrand != null)
                        HttpRuntime.Cache[key] = listBrand;
                }
            }

            List<SelectListItem> result = null;

            if (listBrand != null)
            {
                result = new List<SelectListItem>();
                //result.Add(new SelectListItem
                //{
                //    Value = @"",
                //    Text = @"Bất kì"
                //});

                foreach (var item in listBrand)
                {
                    result.Add(new SelectListItem
                    {
                        Text = item,
                        Value = item
                    });
                }
            }

            return result;
        }
        public List<SelectListItem> GetSeatOption()
        {
            string key =
                "serviceID" + this.ServiceID +
                "GetSeatOption";

            List<string> listSeat = null;
            // Kiểm tra dữ liệu trong cache
            listSeat = (List<string>)HttpRuntime.Cache[key];
            if (listSeat == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    listSeat = this.IGetSeatOption();
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        listSeat = this.IGetSeatOption();
                }
                finally
                {
                    if (listSeat != null)
                        HttpRuntime.Cache[key] = listSeat;
                }
            }

            List<SelectListItem> result = null;

            if (listSeat != null)
            {
                result = new List<SelectListItem>();
                //result.Add(new SelectListItem
                //{
                //    Value = @"",
                //    Text = @"Bất kì"
                //});

                foreach (var item in listSeat)
                {
                    result.Add(new SelectListItem
                    {
                        Text = item,
                        Value = item
                    });
                }
            }

            return result;
        }

        public List<SelectListItem> GetServiceType()
        {
            string key =
                "serviceID" + this.ServiceID +
                "GetServiceType";

            List<string> listService = null;
            // Kiểm tra dữ liệu trong cache
            listService = (List<string>)HttpRuntime.Cache[key];
            if (listService == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    listService = this.IGetServiceType();
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        listService = this.IGetServiceType();
                }
                finally
                {
                    if (listService != null)
                        HttpRuntime.Cache[key] = listService;
                }
            }

            List<SelectListItem> result = null;

            if (listService != null)
            {
                result = new List<SelectListItem>();
                //result.Add(new SelectListItem
                //{
                //    Value = @"",
                //    Text = @"Bất kì"
                //});

                foreach (var item in listService)
                {
                    result.Add(new SelectListItem
                    {
                        Text = item,
                        Value = item
                    });
                }
            }

            return result;
        }
        public List<LoaiXe> SearchForTransportation(
            string region, 
            string classID, 
            string numberOfSeat, 
            string service)
        {
            string key =
                "serviceID" + this.ServiceID +
                "region" + region +
                "classID" + classID +
                "numberOfSeat" + numberOfSeat +
                "serviceType" + service;

            List<LoaiXe> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<LoaiXe>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.ISearchForTransportation(region, classID, numberOfSeat, service);
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.ISearchForTransportation(region, classID, numberOfSeat, service);
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }
        public int? BookingForTransportation(
            string verhicleName, 
            string service,
            DateTime departDate, 
            DateTime returnDate, 
            int driver, 
            int numberOfSeat)
        {
            int? result = null;
            try
            {
                if (!isLogin)
                    this.Login();
                result = this.IBookingForTransportation(verhicleName, service, departDate, returnDate, driver, numberOfSeat);
            }
            catch (Exception ex)
            {
                if (this.Login())
                    result = this.IBookingForTransportation(verhicleName, service, departDate, returnDate, driver, numberOfSeat);
            }

            return result; 
        }
    }
}